(function(factory){
    var nameSpace = 'field_5907643583964200700';
    if(!window[nameSpace]){
        var Builder = factory();
        window[nameSpace] = {
            instance: {}
        };
        window[nameSpace].init = function (options) {
            window[nameSpace].instance[options.privateId] = new Builder(options);
        };
        window[nameSpace].isNotNull = function (obj) {
            return true;
        };
    }
})(function(){
    /**
     * 构造函数
     * @param options
     * @constructor
     */
    function App(options) {
        var self = this;
        //初始化参数
        self.initParams(options);
        //初始化dom
        self.initDom();
        //事件
        self.events();
    }

    App.prototype = {
        initParams : function (options) {
            var self = this;
            self.adaptation = options.adaptation;
            self.privateId = options.privateId;
            self.messageObj = options.getData;
            self.preUrl = options.url_prefix;
        },
        initDom : function () {
            var self = this;
            dynamicLoading.css(self.preUrl + 'css/docFolderFormControls.css');
            self.appendChildDom();
        },
        events : function () {
            var self = this;
            // 监听是否数据刷新
            self.adaptation.ObserverEvent.listen('Event' + self.privateId, function() {
                self.messageObj = self.adaptation.childrenGetData(self.privateId);
                self.appendChildDom();
            });
        },
        appendChildDom : function () {
            var self = this;
            var aclValue = {};
            var canEdit = self.messageObj.auth === 'edit' || self.messageObj.auth === 'add';
            if(self.messageObj.value == ""){//权限申请以外的情况
                aclValue.dId = "fake";
                aclValue.isFolder = true;
                aclValue.isEdoc = false;
                aclValue.aclList = [];
            }else{
                aclValue = JSON.parse(self.messageObj.value);
            }
            var drId = aclValue.dId;
            var domStructure = '<section class="doc_acl_box_content">'+
                '<div class="doc_acl_box_btn"><div class= "'+ self.privateId + '" title="' + self.messageObj.display.escapeHTML() + '">'+ self.messageObj.display.escapeHTML() +'</div></div>'+
                '<div class="doc_acl_list_block" >' +
                '<input type="hidden" id="docAcl_'+ self.privateId +'">' +
                //权限表格
                '<table>' +
                '<thead>' +
                '   <tr>' +
                        createTh(aclValue) +
                '   </tr>' +
                '</thead>' +
                '<tbody id="docAclBody_'+ self.privateId +'">' +
                    createTd(aclValue) +
                '</tbody>' +
                '</table>' +
                //权限表格
                '</div>'+
                '</section>';
            document.querySelector('#' + self.privateId).innerHTML = domStructure;
            var jumpFun = function() {//点击事件
                if(drId === "fake"){//只有权限审批单中可能设权限
                    $.alert($.i18n("doc.acl.action.error"));
                    return;
                }else if(!canEdit){
                    return;
                }
                var _title = '选择权限';
                getA8Top().LibPropWin = getA8Top().$.dialog({
                    title:_title,
                    transParams:{'parentWin':window,'showButton':true,'capAclData':getDocAclListInCap(self.messageObj.value)},
                    url: _ctxPath + "/doc.do?method=docShare&capTag=true&docResId="+drId,
                    width: 700,
                    height: 450,
                    isDrag:false,
                    buttons: [{
                        //id: 'ok',
                        text: "确定",
                        handler: function () {
                            var _aclList = getA8Top().LibPropWin.getReturnValue();
                            document.querySelector('#docAcl_' + self.privateId).value = JSON.stringify(_aclList);
                            aclValue.aclList = _aclList;
                            var listHtml = createTd(aclValue);
                            document.querySelector('#docAclBody_' + self.privateId).innerHTML = listHtml;

                            self.messageObj.showValue = JSON.stringify(aclValue);
                            self.messageObj.value = JSON.stringify(aclValue);
                            self.messageObj.valueId = JSON.stringify(aclValue);
                            self.adaptation.childrenSetData(self.messageObj, self.privateId);
                            self.adaptation.backfillFormControlData(self.messageObj, self.privateId);
                            getA8Top().LibPropWin.close();
                        }
                    }, {
                        //id: 'cancel',
                        //disabled: true,
                        //hide:true,
                        text: "取消",
                        handler: function () {
                            getA8Top().LibPropWin.close();
                        }
                    }]
                });

                function getDocAclListInCap(docAclListJSONStr){
                    var docAclList = [];
                    if(docAclListJSONStr != ""){
                        docAclList = JSON.parse(docAclListJSONStr);
                    }
                    return docAclList;
                }
            };
            document.querySelector('.' + self.privateId).removeEventListener('click',jumpFun);
            document.querySelector('.' + self.privateId).addEventListener('click',jumpFun);
            //渲染隐藏权限
            if (self.messageObj.auth === 'hide') {
                document.querySelector('#' + self.privateId).innerHTML = '<div class="cap4-text__browse" style="line-height: 1.8; color: rgb(0, 0, 0) !important;">***</div>';
            }
        }
    };

    var dynamicLoading = {
        css: function(path) {
            if(!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            var head = document.getElementsByTagName('head')[0];
            var link = document.createElement('link');
            link.href = path;
            link.rel = 'stylesheet';
            link.type = 'text/css';
            head.appendChild(link);
        },
        js: function(path) {
            if(!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            var head = document.getElementsByTagName('head')[0];
            var script = document.createElement('script');
            script.src = path;
            script.type = 'text/javascript';
            head.appendChild(script);
        }
    };

    var createTh = function(aclData){
        var th = '';
        if(aclData.isFolder === true){
            th = '       <th title="授权对象">授权对象</th>' +
                '       <th title="全部">全部</th>' +
                '       <th title="列表">列表</th>' +
                '       <th title="浏览">浏览</th>' +
                '       <th title="新建">新建</th>' +
                '       <th title="编辑">编辑</th>' +
                '       <th title="删除">删除</th>' +
                '       <th title="打印">打印</th>' +
                '       <th title="下载">下载</th>' +
                '       <th title="转发">转发</th>' +
                '       <th title="公开到">公开到</th>' +
                '       <th title="新增消息通知">新增消息通知</th>';
        }else if(aclData.isEdoc === true){
            th = '       <th title="授权对象">授权对象</th>' +
                '       <th title="借阅权限">借阅权限</th>' +
                '       <th title="打印">打印</th>' +
                '       <th title="保存">保存</th>' +
                '       <th title="开始时间">开始时间</th>' +
                '       <th title="结束时间">结束时间</th>';
        }else{
            th = '       <th title="授权对象">授权对象</th>' +
                '       <th title="浏览">浏览</th>' +
                '       <th title="转发">转发</th>' +
                '       <th title="打印">打印</th>' +
                '       <th title="下载">下载</th>' +
                '       <th title="开始时间">开始时间</th>' +
                '       <th title="结束时间">结束时间</th>';
        }
        return th;
    };
    var createTd = function(aclValue){
        var td = "";
        if(aclValue.dId == "fake"){
            return td;
        }
        var _aclList = aclValue.aclList;

        for(var i = 0; i < _aclList.length; i++){
            var aclValueSplit = _aclList[i].split("|");
            var userId = aclValueSplit[0];
            var userName = '<td>' + aclValueSplit[1] + '</td>';
            var userType = aclValueSplit[2];
            var aclStr = aclValueSplit[3].toCharArray();
            if(aclValueSplit[3].startsWith("0000000000")){
                continue;
            }
            if(aclValue.isFolder){
                var _all = '<td>' + (aclStr[0] == 1? "√" : "") + '</td>';
                var _list = '<td>' + (aclStr[1] == 1? "√" : "") + '</td>';
                var _read = '<td>' + (aclStr[2] == 1? "√" : "") + '</td>';
                var _add = '<td>' + (aclStr[3] == 1? "√" : "") + '</td>';
                var _edit = '<td>' + (aclStr[4] == 1? "√" : "") + '</td>';
                var _del = '<td>' + (aclStr[5] == 1? "√" : "") + '</td>';
                var _print = '<td>' + (aclStr[6] == 1? "√" : "") + '</td>';
                var _download = '<td>' + (aclStr[7] == 1? "√" : "") + '</td>';
                var _lend = '<td>' + (aclStr[8] == 1? "√" : "") + '</td>';
                var _openToZone = '<td>' + (aclStr[9] == 1? "√" : "") + '</td>';
                var _alert = '<td>' + (aclStr[10] == 1? "√" : "") + '</td>';
                var _rowHtml = "<tr>" + userName;
                _rowHtml += _all + _list;
                _rowHtml += _read + _add;
                _rowHtml += _edit + _del;
                _rowHtml += _print + _download;
                _rowHtml += _lend + _openToZone;
                _rowHtml += _alert
            }else if(aclValue.isEdoc){
                var _lenPotent = '<td>' + (aclValueSplit[6] == 1? "全部信息" : "公文正文") + '</td>';
                var _print = '<td>' + (aclStr[6] == 1? "√" : "") + '</td>';
                var _download = '<td>' + (aclStr[7] == 1? "√" : "") + '</td>';
                var _start = '<td>' + (aclValueSplit[4] == "9777"? "无" : aclValueSplit[4]) + '</td>';
                var _end = '<td>' + (aclValueSplit[5] == "9777"? "无" : aclValueSplit[5]) + '</td>';
                var _rowHtml = "<tr>" + userName;
                _rowHtml += _lenPotent;
                _rowHtml += _print + _download;
                _rowHtml += _start + _end;
            }else {
                var _read = '<td>' + (aclStr[1] == 1? "√" : "") + '</td>';
                var _lend = '<td>' + (aclStr[8] == 1? "√" : "") + '</td>';
                var _print = '<td>' + (aclStr[6] == 1? "√" : "") + '</td>';
                var _download = '<td>' + (aclStr[7] == 1? "√" : "") + '</td>';
                var _start = '<td>' + (aclValueSplit[4] == "9777"? "无" : aclValueSplit[4]) + '</td>';
                var _end = '<td>' + (aclValueSplit[5] == "9777"? "无" : aclValueSplit[5]) + '</td>';
                var _rowHtml = "<tr>" + userName;
                _rowHtml += _read;
                _rowHtml += _lend;
                _rowHtml += _print + _download;
                _rowHtml += _start + _end;
            }

            _rowHtml += "</tr>";
            td += _rowHtml;
        }
        return td;
    };
    return App;
});